import { request } from "../../request/index.js";
Page({
  data: {
    tabs:[
      {
        id:0,
        value:"全部",
        isActive:true
      },
      {
        id:1,
        value:"待付款",
        isActive:false
      },
      {
        id:2,
        value:"待发货",
        isActive:false
      },
      {
        id:3,
        value:"退款/退货",
        isActive:false
      }
    ],
    orders:[]
  },
  onShow: function(options){
    const token=wx.getStorageSync("token");//自己编的
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/auth',
      });
      return;
    }
    //1 获取当前的小程序的页面栈-数组，长度最大为10
    let pages =  getCurrentPages();
    //2 数组中 索引最大的页面就是当前页面
    let currentPage=pages[pages.length-1];
    //console.log(currentPage.options);
    //3 获取url上的type参数
    const {type}=currentPage.options;
    //4 激活选中的页面标题
    this.changeTitleByIndex(type-1);
    this.getOrders(type);
  },
  //根据标题来激活选中
  changeTitleByIndex(index){
    //修改原数组
    let {tabs}=this.data;
    tabs.forEach((v,i) => {
      i===index?v.isActive=true:v.isActive=false;
    });
    //赋值到data中
    this.setData({
      tabs
    })
  },
  //标题点击事件
  handleTabsItemChange(e){
    //1 获取点击索引
    const {index}=e.detail;
    this.changeTitleByIndex(index);
    //2 重新发送请求
    this.getOrders(index+1);
  },
  //获取订单列表的方法
  async getOrders(type){
    let res=await request({url:"my/orders/all",data:{type}});
    //console.log(res);
    //对获取的值自己编
    res={
      count:4,
      orders:[
        {
          order_id:23,
          order_name:"HMDD8848884888848484848",
          order_price:9999,
          date:"2019-11-12 下午9:36:25"
        },
        {
          order_id:23,
          order_name:"HMDD884811012013011011",
          order_price:6999,
          date:"2019-1-12 下午9:36:25"
        },
        {
          order_id:23,
          order_name:"HMDD884811015484848881",
          order_price:7999,
          date:"2018-11-12 下午9:36:25"
        },
        {
          order_id:23,
          order_name:"HMDD88481115155441011",
          order_price:8999,
          date:"2018-10-12 下午9:36:25"
        }
      ]
    };
    //console.log(currentPage);
    //进行赋值
    this.setData({
      orders:res.orders
    })
  }
});